<template>
  <div class="login-account">
    <el-form :model="account" label-width="70px" :rules="rules" ref="formRef">
      <el-form-item label="账号：" prop="name">
        <el-input v-model="account.name"/>
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input v-model="account.password" show-password/>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../config/account-config'
import type { ElForm } from 'element-plus/lib/components'
import localCache from '@/utils/localCache'
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    const store = useStore()
    
    const account = reactive({
      name: localCache.getCache('name') ?? '',
      password: localCache.getCache('password') ?? ''
    })

    const formRef = ref<InstanceType<typeof ElForm>>()

    const loginAction = (isSavePassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          if (isSavePassword) {
            localCache.setCache('name', account.name)
            localCache.setCache('password', account.password)
          } else {
            localCache.removeCache('name')
            localCache.removeCache('password')
          }

          // 页面跳转登录
          store.dispatch('login/accountLoginAction', { ...account })
        }
      })
    }

    return {
      account,
      rules,
      formRef,
      loginAction
    }
  }
})
</script>

<style scoped lang='less'>
.login-panel {}
</style>